package com.yanqu.road.server.manger.maidian.medicineroom;

import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.drugstore.DrugHardCaseRecordData;
import com.yanqu.road.entity.drugstore.DrugStoreTeach;
import com.yanqu.road.entity.drugstore.UserDrugChuFangData;
import com.yanqu.road.entity.drugstore.UserDrugStoreData;
import com.yanqu.road.entity.drugstore.config.DrugStoreDrugUpgradeInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.drugstore.DrugStoreModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.drugstore.DrugStoreConfigMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MedicineRoom {

    public static final String MEDICINE_ROOM = "medicine_room";
    public static final String MEDICINE_ART = "medicine_art";
    public static final String MEDICINE_PLAN = "medicine_plan";
    public static final String MEDICINE_ACHIEVE = "medicine_achieve";
    public static final String MEDICINE_HARD = "medicine_hard";
    public static final String MEDICINE_LEVEL = "medicine_level";

    public static final String HOSPITAL_CASENAME = "hospital_casename";
    public static final String HOSPITAL_CASELV = "hospital_caselv";
    public static final String VOCATION = "vocation";
    public static final String HOSPITAL_CASEADD = "hospital_caseadd";

    public static final String MEDICINE_ARTNAME = "medicine_artlv";
    public static final String MEDICINE_ARTLV = "medicine_artlv";
    public static final String MEDICINE_ARTADD = "medicine_artadd";

    public static final String MEDICINE_PLANNAME = "medicine_planname";
    public static final String MEDICINE_PLANLV = "medicine_planlv";

    public static final String MEDICINE_PLANLVSUM = "medicine_planlvsum";
    public static final String MEDICINE_SUM = "medicine_sum";
    public static final String MEDICINE_ARTLVSUM = "medicine_artlvsum";
    public static final String MEDICINE_ID = "medicine_id";
    public static final String MEDICINE_ISLOCK = "medicine_islock";
    public static final String MEDICINE_SCORE = "medicine_score";

    public static void uploadSnapshot(GamePlayer player) {

        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;
            DrugStoreModule drugStoreModule = player.getModule(DrugStoreModule.class);
            UserDrugStoreData userDrugStoreData = drugStoreModule.getUserDrugStoreData();
            if(userDrugStoreData == null){
                return;
            }

            DefaultEventTraceAction action = new DefaultEventTraceAction(MEDICINE_ROOM, player);

            List<Map<String, Object>> objectGroup = new ArrayList<>();

            int artLvSum = 0;
            //工艺
            for (int i = 0; i < userDrugStoreData.getTechList().size(); i++) {
                DrugStoreTeach teach = userDrugStoreData.getTechList().get(i);
                Map<String, Object> map = new HashMap<>();
                map.put(HOSPITAL_CASENAME, teach.getId());
                map.put(HOSPITAL_CASELV, teach.getLv());
                artLvSum += teach.getLv();
                int vocation = 0;
                int addition = 0;
                DrugStoreDrugUpgradeInfo upgradeInfo = DrugStoreConfigMgr.getDrugStoreDrugUpgradeInfo(teach.getId(), teach.getLv());
                if(upgradeInfo != null){
                    for (int skillId : upgradeInfo.getSkillList()) {
                        SkillInfo skillInfo = SkillMgr.getSkillInfo(skillId);
                        if (skillInfo != null) {
                            addition += SkillMgr.getSkillAddition(skillInfo, teach.getLv());
                            vocation = skillInfo.getParamList().get(2);
                        }
                    }
                }
                map.put(VOCATION, vocation);
                map.put(HOSPITAL_CASEADD, addition);
                objectGroup.add(map);
            }
            action.build(MEDICINE_ART, objectGroup);

            int planLvSum = 0;
            objectGroup = new ArrayList<>();
            for (Map.Entry<Integer, UserDrugChuFangData> dataEntry : drugStoreModule.getChuFangDataMap().entrySet()) {
                UserDrugChuFangData chuFangData = dataEntry.getValue();
                Map<String, Object> map = new HashMap<>();
                map.put(MEDICINE_PLANNAME, chuFangData.getChuFang());
                map.put(MEDICINE_PLANLV, chuFangData.getLv());
                planLvSum += chuFangData.getLv();
                objectGroup.add(map);
            }
            action.build(MEDICINE_PLAN, objectGroup);

            //成就
            List<Integer> list = new ArrayList<>();
            list.add(planLvSum);
            list.add((int)userDrugStoreData.getReceiveNum());
            list.add(artLvSum);

            action.build(MEDICINE_ACHIEVE, list);

            //疑难杂症
            objectGroup = new ArrayList<>();
            for (Map.Entry<Integer, List<DrugHardCaseRecordData>> listEntry : drugStoreModule.getHardCaseRecordListMap().entrySet()) {
                for (int i = 0; i < listEntry.getValue().size(); i++) {
                    DrugHardCaseRecordData caseRecordData = listEntry.getValue().get(i);
                    Map<String, Object> map = new HashMap<>();
                    map.put(MEDICINE_ID, caseRecordData.getCaseId());
                    map.put(MEDICINE_SCORE, caseRecordData.getScore());
                    objectGroup.add(map);
                }
            }
            action.build(MEDICINE_HARD, objectGroup);

            //勋章
            action.build(MEDICINE_LEVEL, userDrugStoreData.getDrugStoreLv());

            action.trace();

        } catch (Exception e) {
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }

    }
}
